﻿using Electronic_work_order.JWT;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Electronic_work_order.Model;
using Electronic_work_order.Interfacess;

namespace Electronic_work_order.Controllers
{
    [EnableCors("any")]


    [Route("api/[controller]")]

    [Route("api/[controller]/[action]")]


    [Route("api/[controller]")]
    [Route("api/[controller]/[action]")]

    [ApiController]

    public class OASystemController : ControllerBase
    {
        private readonly IUsers sqls;
        private readonly ITokenHelper tokenHelper = null;


        //构造函数
        public OASystemController(ITokenHelper _tokenHelper,IUsers sqls)

        {
            this.sqls = sqls;
     
            tokenHelper = _tokenHelper;
            this.sqls = sqls;
        }

     

        //登陆    
        /// <summary>
        /// 登录测试
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("/GetLogin")]
        public ReturnModel GetLogin(string UsersName, string UsersPwd)
        {
           
            var ret = new ReturnModel();
            try
            {
                if (string.IsNullOrWhiteSpace(UsersName) || string.IsNullOrWhiteSpace(UsersPwd))
                {
                    ret.Code = 201;
                    ret.Msg = "用户名密码不能为空";
                    return ret;
                }

            


                
                //登录操作  假设登录成功
                if (sqls.GetLogins(UsersName, UsersPwd) > 0)

                {
                    Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
                    {
                        { "UsersName",UsersName }
                    };
                    ret.Code = 200;
                    ret.Msg = "登录成功";
                    ret.TnToken = tokenHelper.CreateToken(keyValuePairs);
                        // ret.Data ={ "UsersName":na}
                        ret.Data = sqls.GetUsers(UsersName, UsersPwd);
                    }
                else
                {
                    ret.Code = 201;
                    ret.Msg = "用户名或密码错误";
                }
            }
            catch (Exception ex)
            {
                ret.Code = 500;
                ret.Msg = "登录失败:" + ex.Message;
            }
            return ret;
        }

       
        /// <summary>
        /// 验证Token 带返回状态
        /// </summary>
        /// <param name="tokenStr"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("/ValiTokenState")]
        public ReturnModel ValiTokenState(string tokenStr)
        {
            var ret = new ReturnModel
            {
                TnToken = new TnToken()
            };
            string UsersName = "";
            TokenType tokenType = tokenHelper.ValiTokenState(tokenStr, action => { UsersName = action["UsersName"]; });
            if (tokenType == TokenType.Fail)
            {
                ret.Code = 202;
                ret.Msg = "token验证失败";
                return ret;
            }
            if (tokenType == TokenType.Expired)
            {
                ret.Code = 205;
                ret.Msg = "token已经过期";
                return ret;
            }

            //..............其他逻辑
            var data = new List<Dictionary<string, string>>();
            var bb = new Dictionary<string, string>
            {
                { "Wyy", "123456" }
            };
            data.Add(bb);
            ret.Code = 200;
            ret.Msg = "访问成功!";
            ret.Data = data;
            return ret;
        }




       
    }

}
